package com.xinfan.tank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import static java.awt.event.KeyEvent.VK_DOWN;

/**
 * @Author xin
 * @Date 2022/5/31 17:43
 * @Version 1.0
 */
public class BallMove extends JFrame{
    BallDraw ballDraw = null;
    public static void main(String[] args) {
        new BallMove();
    }

    public BallMove() {
        ballDraw = new BallDraw();
        this.add(ballDraw);

        this.addKeyListener(ballDraw);
        this.setSize(1000,750);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

class BallDraw extends JPanel implements KeyListener {
    int x = 10;
    int y = 10;

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillOval(x,y,10,10);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if ((char)e.getKeyCode() == VK_DOWN){
            y++;
            repaint();
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
